package com.ktwlsoft.archivemanage.controller;

import com.ktwlsoft.archivemanage.domain.business.businessService.RoleService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.RoleRepositoryService;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.role.AddRoleRequest;
import com.ktwlsoft.archivemanage.payload.role.EditRoleRequest;
import com.ktwlsoft.archivemanage.payload.role.QueryRoleRequest;
import com.ktwlsoft.archivemanage.service.security.UserAuthenticate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(value = "角色管理", description = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController {

    private final RoleRepositoryService roleRepositoryService;

    private final RoleService roleService;

    public RoleController(RoleRepositoryService roleRepositoryService, RoleService roleService) {
        this.roleRepositoryService = roleRepositoryService;
        this.roleService = roleService;
    }

    @PostMapping("/addRole")
    @ApiOperation("添加角色")
    @UserAuthenticate(actionCode = "ROLE_ADD")
    public ResponseEntity<?> addRole(@Valid @RequestBody AddRoleRequest request) {
        roleRepositoryService.insert(request);
        return ResponseEntity.ok(Result.ok("操作完成"));
    }

    @PostMapping("/updateRole")
    @ApiOperation("编辑角色")
    @UserAuthenticate(actionCode = "ROLE_EDIT")
    public ResponseEntity<?> editRole(@Valid @RequestBody EditRoleRequest request) {
        roleService.updateRole(request);
        return ResponseEntity.ok(Result.ok("操作完成"));
    }

    @DeleteMapping("/deleteRole/{roleId}")
    @ApiOperation("删除角色")
    @UserAuthenticate(actionCode = "ROLE_DELETE")
    public ResponseEntity<?> deleteRole(@PathVariable long roleId) {
        roleService.deleteRole(roleId);
        return ResponseEntity.ok(Result.ok("操作完成"));
    }

    @GetMapping("/findList")
    @ApiOperation("分页列表")
    public ResponseEntity<?> findList(@Valid QueryRoleRequest request) {
        return ResponseEntity.ok(Result.ok(roleRepositoryService.findPage(request)));
    }
}
